from django.db import models


class Category(models.Model):
    """新闻分类"""
    name = models.CharField(null=False, max_length=20, verbose_name='分类名')  # 分类名
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = "info_category"
        verbose_name = "新闻分类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class News(models.Model):
    user = models.ForeignKey('users.User', null=True, related_name='news_list', verbose_name='用户发布的新闻')
    category = models.ForeignKey('news.Category', related_name='news', verbose_name='所属分类')
    title = models.CharField(max_length=256, null=False)  # 新闻标题
    source = models.CharField(max_length=64, null=False)  # 新闻来源
    digest = models.CharField(max_length=512, null=False)  # 新闻摘要
    content = models.TextField(null=False)  # 新闻内容
    clicks = models.IntegerField(default=0)  # 浏览量
    index_image_url = models.CharField(max_length=256)  # 新闻列表图片路径
    status = models.IntegerField(default=0)  # 当前新闻状态 如果为0代表审核通过，1代表审核中，-1代表审核不通过
    reason = models.CharField(max_length=256)  # 未通过原因，status = -1 的时候使用
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = "info_news"
        verbose_name = "新闻"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class UserCollection(models.Model):
    user = models.ForeignKey('users.User',related_name='collects', verbose_name="所属用户")
    news = models.ForeignKey('news.News', verbose_name="所属新闻")

    class Meta:
        db_table = "info_user_collection"
        verbose_name = "收藏新闻表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.username


class Comment(models.Model):
    """评论"""
    user = models.ForeignKey('users.User', related_name='comment', verbose_name='谁评论的')
    news = models.ForeignKey('news.News', related_name='comments', verbose_name='评论的新闻')
    parent = models.ForeignKey("self", null=True, blank=True, verbose_name='父评论')  # 自关联
    content = models.TextField(null=False)  # 评论内容
    like_count = models.IntegerField(default=0)  # 点赞条数
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = "info_comment"
        verbose_name = "评论"
        verbose_name_plural = verbose_name

    def to_dict(self):
        resp_dict = {
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "content": self.content,
            "parent": self.parent.to_dict() if self.parent else None,
            "like_count": self.like_count
        }
        return resp_dict
    def __str__(self):
        return self.content


class CommentLike(models.Model):
    """点赞"""
    user = models.ForeignKey("users.User", verbose_name="点赞用户")
    comment = models.ForeignKey("news.Comment", verbose_name="点赞评论")

    class Meta:
        db_table = "info_comment_like"
        verbose_name = "点赞"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s:%s' % (self.user_id, self.comment_id)


class UserFans(models.Model):
    fans = models.ForeignKey('users.User', related_name="followers", verbose_name="粉丝")
    follow = models.ForeignKey('users.User', related_name="follow", verbose_name="被关注人")

    class Meta:
        db_table = "info_user_fans"
        verbose_name = "粉丝"
        verbose_name_plural = verbose_name

    def __str__(self):
        return
